package game;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: liwan
 * @Description:
 * @Date: create in 2021/9/2 15:50:31
 */
public class GamePanel extends JPanel {
    private List<BufferedImage> backImg = new ArrayList<>();  // 图片背景
    private int backNum = 4;    //  背景图片个数
    private int backIndex = 1;  //  背景图片序号
    private Hero hero;
    private HeadPic headPic;
    private HeroBlood heroBlood;
    private HeroBule heroBule;
    private List<Enemy> enemy = new ArrayList<>();
    private int enemyNum = 200;
    private boolean warry = false;

    public GamePanel() {
        this.hero = new Hero(this);
        this.headPic = new HeadPic();
        this.heroBlood = new HeroBlood(this, headPic.getW(), headPic.getH());
        this.heroBule = new HeroBule(this, headPic.getW(), headPic.getH());
        for (int i = 0; i < this.enemyNum; i++) {
            this.enemy.add(new Enemy(this, this.hero, this.heroBlood));
        }
        for (int i = 1; i <= backNum; i++) {
            backImg.add(ImageUtil.getImg("../img/bg" + i + ".jpg"));
        }
        for (Enemy enemy1 : this.enemy) {
            enemy1.start();
        }
        heroBule.start();
        heroBlood.start();
    }


    @Override
    public void paint(Graphics g) {
        g.drawImage(this.backImg.get(this.backIndex), 0, 0, GameFrame.W, GameFrame.H, null);
        g.drawImage(this.hero.getImage(), this.hero.getX(), this.hero.getY(), this.hero.getW(), this.hero.getH(), null);
        g.drawImage(this.headPic.getImage(), this.headPic.getX(), this.headPic.getY(), this.headPic.getW(), this.headPic.getH(), null);
        g.setColor(new Color(0xEE0C0C));
        for (int i = 0; i < this.enemyNum; i++) {
            g.fillRect(this.enemy.get(i).getX(), this.enemy.get(i).getY() - this.enemy.get(i).getH() / 4, this.enemy.get(i).getBloodW(), this.enemy.get(i).getBloodH());
            g.drawRect(this.enemy.get(i).getX(), this.enemy.get(i).getY() - this.enemy.get(i).getH() / 4, 100, this.enemy.get(i).getBloodH());
            g.drawImage(this.enemy.get(i).getImage(), this.enemy.get(i).getX(), this.enemy.get(i).getY(), this.enemy.get(i).getW(), this.enemy.get(i).getH(), null);
        }
        g.fillRect(heroBlood.getX(),heroBlood.getY(),heroBlood.getW(),heroBlood.getH());
        g.drawRect(heroBlood.getX(),heroBlood.getY(),heroBlood.getW(),heroBlood.getH());
        g.setColor(new Color(0x0420D9));
        g.fillRect(heroBule.getX(),heroBule.getY(),heroBule.getW(),heroBule.getH());
        g.drawRect(heroBule.getX(),heroBule.getY(),500,heroBule.getH());
        if (this.warry) {
            g.setColor(new Color(0xEE0C0C));
            g.setFont(new Font("黑体",Font.BOLD,50));
            g.fillRect(0,0,GameFrame.W,GameFrame.H);
            g.setColor(new Color(0x188A9E));
            g.drawString("game over", 720, 330);
        }
    }


    public int getBackIndex() {
        return backIndex;
    }

    public void setBackIndex(int backIndex) {
        this.backIndex = backIndex;
    }

    public int getBackNum() {
        return backNum;
    }

    public void setBackNum(int backNum) {
        this.backNum = backNum;
    }

    public Hero getHero() {
        return hero;
    }

    public void setHero(Hero hero) {
        this.hero = hero;
    }

    public HeadPic getHeadPic() {
        return headPic;
    }

    public void setHeadPic(HeadPic headPic) {
        this.headPic = headPic;
    }

    public HeroBlood getHeroBlood() {
        return heroBlood;
    }

    public void setHeroBlood(HeroBlood heroBlood) {
        this.heroBlood = heroBlood;
    }

    public HeroBule getHeroBule() {
        return heroBule;
    }

    public void setHeroBule(HeroBule heroBule) {
        this.heroBule = heroBule;
    }

    public List<Enemy> getEnemy() {
        return enemy;
    }

    public void setEnemy(List<Enemy> enemy) {
        this.enemy = enemy;
    }

    public int getEnemyNum() {
        return enemyNum;
    }

    public void setEnemyNum(int enemyNum) {
        this.enemyNum = enemyNum;
    }

    public boolean isWarry() {
        return warry;
    }

    public void setWarry(boolean warry) {
        this.warry = warry;
    }
}
